package com.hyf.serviceedu.listenersExcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyf.service.config.exception.GuliException;
import com.hyf.serviceedu.entity.EduSubject;
import com.hyf.serviceedu.entity.excel.SubjectDataExcel;
import com.hyf.serviceedu.service.EduSubjectService;

public class SubjectExcelListener extends AnalysisEventListener<SubjectDataExcel> {

    public EduSubjectService subjectService;// 手动注入 service

    public SubjectExcelListener() {// 无参的构造函数
    }
        //创建有参数构造，传递subjectService用于操作数据库
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService = subjectService;// 有参的构造函数
    }



    // 读取excel 内容，一行一行读取
    @Override
    public void invoke(SubjectDataExcel data, AnalysisContext context) {
        if(data==null){// data 等于空的话表示里面没有数据
                 throw  new GuliException(20001,"数据读取异常");
        }

        // 因为是一行一行读取的，第一个是一级分类，第二个是二级分类
        // 先判断一级分类是否重复
        EduSubject existOneSubject = this.existOneSubject(subjectService, data.getOneSubjectName());
        if(existOneSubject==null){// 要是 existOneSubject 等于空就表示，没有相同的一级分类，进行添加
            existOneSubject=new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(data.getOneSubjectName());// 一级分类名称
            subjectService.save(existOneSubject);// 一级分类添加

        }

        // 添加二级分类
        String pid=existOneSubject.getId();// 拿到 一级分类的id 值
        System.out.println(pid);// 打印一下
        EduSubject existTwoSubject = this.existTwoSubject(subjectService, data.getTowSubjectName(), pid);// 二级分类的一个判断值
        if(existTwoSubject==null){
            existTwoSubject=new EduSubject();
            existTwoSubject.setParentId(pid);
            existTwoSubject.setTitle(data.getTowSubjectName());// 二级分类名称
            subjectService.save(existTwoSubject);
        }


    }




    // 判断一级分类不能重复添加
    private EduSubject existOneSubject(EduSubjectService subjectService,String name){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject one = subjectService.getOne(wrapper);// 调用查询方法
        return one;

    }
    // 判断二级分类不能重复添加
    private EduSubject existTwoSubject(EduSubjectService subjectService,String name,String pid){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject Two = subjectService.getOne(wrapper);// 调用查询方法
        return Two;

    }

    @Override//读取完成后执行
    public void doAfterAllAnalysed(AnalysisContext context) {

    }





/*
    // 因为 SubjectExcelListener 不能交给 spring 进行 管理，需要自己 new ，不能注入其他对象
    // 不能实现数据库操作 通过手动操作 把service 传过来
    public EduSubjectService eduSubjectService;

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }
    public SubjectExcelListener() {
    }

    @Override// 读取 excel 内容，一行一行读取
    public void invoke(SubjectDataExcel data, AnalysisContext context) {

        if(data==null){
            throw new GuliException(20001,"文件数据唯恐");
        }
        // 因为是一行一行读取的，每次读取都有两个值，第一个是 一级分类
        // 判断一级分类是否重复
        EduSubject existoneeduSubject = this.existOneSubject(data.getOneSubjectName(), eduSubjectService);
        if(existoneeduSubject==null){// 表示没有相同的一级分类
            existoneeduSubject=new EduSubject();
            existoneeduSubject.setParentId("0");
            existoneeduSubject.setTitle(data.getOneSubjectName());
            eduSubjectService.save(existoneeduSubject);

        }
     // 添加二级分类
        // 判断二级分类是否重复\
        String pid=existoneeduSubject.getId();// 获取一级分类的id
        EduSubject existTowSubject = this.existTowSubject(eduSubjectService, data.getTowSubjectName(), pid);
        if(existTowSubject==null){
            existTowSubject=new EduSubject();
            existTowSubject.setParentId(pid);
            existTowSubject.setTitle(data.getTowSubjectName());// 二级分类名称
            eduSubjectService.save(existTowSubject);
        }


    }
    // 判断一级分类不能重复添加 //判断一级分类是否重复
    private EduSubject existOneSubject(String name,EduSubjectService eduSubjectService){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);
        return oneSubject;
    }

    // 判断二级分类不能重复
    private EduSubject existTowSubject(EduSubjectService eduSubjectService,String name,String pid){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject twoSubject = eduSubjectService.getOne(wrapper);
        return twoSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }*/
}
